package com.example.demo02.service;

import com.example.demo02.bean.Books;
import com.example.demo02.mapper.BooksMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @Author liuyuan
 * @Date 2022/11/9 9:40 AM
 * @Version 1.0
 */
@Service
public class BooksServiceImpl implements BooksService{
    @Autowired
    BooksMapper booksMapper;

    @Override
    public ArrayList<Books> query() {
        return null;
    }
    /**
     * 添加书名
     * 1.添加书的时候，要判读这本书名在数据库有没有存在
     *
     * 2.如果存在，提示此书已存在，不能重复添加
     * 3.如果不存在，就进行添加，提示添加成功
     * */
    @Override
    public boolean insert(Books books) {
        String name = books.getName();
        Books byName = booksMapper.getByName(name);
        if(byName == null){
            booksMapper.insert(books);
            Long id = books.getId();
            System.out.println("-------添加了一条id为:"+id+"书。");
            return true;
        }else{
            return false;
        }

    }
}
